﻿#!/usr/bin/env python
#coding:utf-8

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from app.extended import ProfileUser
from django.utils import timezone

class MaintenanceMethod(models.Model):
    """维护工作处理方式（当场，转存计划，转交）"""
    name = models.CharField(max_length = 12,  verbose_name='处理方式')
    description = models.CharField(max_length = 50,  verbose_name='说明')

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'maintenance_method'
        verbose_name = '维护方式'
        verbose_name_plural = '维护方式'

class AppSettings(models.Model):
    """系统设置"""
    name = models.CharField(max_length=50, verbose_name="Name")
    value = models.CharField(max_length=200, verbose_name="Value")
    description = models.CharField(max_length=500, verbose_name="Description")
    lastEditor = models.ForeignKey(User, verbose_name="User")

    class Meta:
        db_table = 'settings'
        verbose_name = '系统设置'
        verbose_name_plural = '系统设置'

class ProjectStatus(models.Model):
    """项目状态"""
    # 进行中, 暂停, 恢复, 结项, 非正常终止 等
    name = models.CharField(max_length=20, verbose_name='状态')
    description = models.CharField(max_length=200, default='', blank=True, verbose_name='说明')

    class Meta:
        db_table = 'project_status'
        verbose_name = '项目状态'
        verbose_name_plural = '项目状态'

class Projects(models.Model):
    """项目信息表，记录开发周期中的项目信息"""
    name = models.CharField(max_length=20, verbose_name='项目名称')
    leader = models.ForeignKey(User, verbose_name="项目经理", related_name = 'project_leader')
    start_date = models.DateField(verbose_name = '开始时间')
    end_date = models.DateField(verbose_name = '(预计)结束时间')
    employees = models.ForeignKey(User, verbose_name = '组员列表', default='', related_name = 'project_member')

    class Meta:
        db_table = 'projects'
        verbose_name = '项目信息'
        verbose_name_plural = '项目信息'

class ProjectStatusHistory(models.Model):
    """项目状态流水"""
    project = models.ForeignKey(Projects, verbose_name = '项目')
    status = models.ForeignKey(ProjectStatus, verbose_name = '状态')
    status_memo = models.CharField(verbose_name = '项目当前状态说明', max_length=500, default='', blank=True)
    status_changed_date = models.DateField(verbose_name = '状态变更日期', blank=False, default=timezone.now)
    reg_date = models.DateField(verbose_name = '登记日期', default=timezone.now)

    class Meta:
        db_table = 'project_status_history'
        verbose_name = '项目状态流水'
        verbose_name_plural = '项目状态流水'
        unique_together = (("project","status"),)

class UserDepartment(models.Model):
    """部门"""
    name = models.CharField(max_length=20, verbose_name='部门名称')
    prefix = models.CharField(max_length=5, verbose_name='日志前缀')
    description = models.CharField(max_length=200, verbose_name='部门简介/职能描述')

    class Meta:
        db_table = 'user_department'
        verbose_name = '部门'
        verbose_name_plural = '部门'

class DiaryMain(models.Model):
    """日志索引"""
    date = models.DateField(verbose_name = '日期')
    user = models.ForeignKey(User, verbose_name = '用户')

    class Meta:
        db_table = 'diary_main'
        verbose_name = '日志索引'
        verbose_name_plural = '日志索引'

class DiaryDevelopment(models.Model):
    """开发工作日志"""
    diary = models.ForeignKey(DiaryMain)
    d_name = models.ForeignKey(User,verbose_name='任务指派人/项目名称/个人计划')
    d_plan = models.CharField(max_length=500, verbose_name='计划任务')
    d_performance = models.CharField(max_length=500, verbose_name='完成情况')
    d_unfinished_reason = models.CharField(max_length=500, verbose_name='未完成原因')
    d_pre_time = models.CharField(max_length=10, verbose_name='预计增时')

    class Meta:
        db_table = 'diary_development'
        verbose_name = '开发工作日志'
        verbose_name_plural = '开发工作日志'

class DiaryMaintenance(models.Model):
    """维护工作日志"""
    diary = models.ForeignKey(DiaryMain)
    m_id = models.CharField(max_length=2, verbose_name='维护编号')
    m_type = models.CharField(max_length=2, verbose_name=' 维护类型')
    m_client = models.CharField(max_length=50, verbose_name='客户单位')
    m_product = models.ForeignKey(Projects, verbose_name = '产品')
    m_contracts = models.CharField(max_length=50, verbose_name='联系人')
    m_contract_information = models.CharField(max_length=50, verbose_name='联系方式')
    m_question = models.CharField(max_length=100, verbose_name='客户问题')
    m_q_timelimit = models.CharField(max_length=100, verbose_name='要求时间')
    m_process_content = models.CharField(max_length=500, verbose_name='处理内容')
    m_process_type = models.ForeignKey(MaintenanceMethod, verbose_name='处理方式')
    m_hand_to = models.ForeignKey(User, verbose_name='转交人')
    m_resolved = models.BooleanField(verbose_name='是否解决')

    class Meta:
        db_table = 'diary_maintenance'
        verbose_name = '维护工作日志'
        verbose_name_plural = '维护工作日志'

class Educations(models.Model):
    """学历"""
    name = models.CharField(max_length=20, verbose_name='名称', blank=False, default='')
    description = models.CharField(max_length=200, verbose_name='描述', default='', blank=True)

    class Meta:
        db_table = 'educations'
        verbose_name = '学历'
        verbose_name_plural = '学历'

class Functions(models.Model):
    """系统功能表"""
    # 该表应通过脚本自动添加和维护，尽量避免手工维护
    name = models.CharField(max_length=20, verbose_name='名称', blank=False, default='')
    description = models.CharField(max_length=200, verbose_name='描述', default='', blank=True)
    url = models.URLField(verbose_name='地址', default='', blank=False)
    cls_name = models.CharField(max_length=100, verbose_name='类名', default='', blank=True)
    def_name = models.CharField(max_length=100, verbose_name='方法名', default='', blank=True)
    valid = models.BooleanField(verbose_name='是否可用', default=True)
    add_date = models.DateField(verbose_name='添加日期', default=timezone.now,blank=False)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'functions'
        verbose_name = '系统功能表'
        verbose_name_plural = '系统功能表'

class PermissionGroup(models.Model):
    """权限组"""
    name = models.CharField(max_length=20, verbose_name='名称', blank=False, default='')
    description = models.CharField(max_length=200, verbose_name='描述', default='', blank=True)
    functions = models.ManyToManyField(Functions, verbose_name='权限功能', blank=False)
    diary_write = models.BooleanField(verbose_name='是否需要写日志', default=True)
    weekly_report = models.BooleanField(verbose_name='是否需要写周报',default=False)
    valid = models.BooleanField(verbose_name='是否可用', default=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'permission_group'
        verbose_name = '权限组'
        verbose_name_plural = '权限组'

class Posts(models.Model):
    """职位/岗位"""
    default_pk = 1
    name = models.CharField(max_length=20, verbose_name='岗位名称')
    description = models.CharField(max_length=200, default='', blank=True, verbose_name='岗位职能描述')
    permission = models.ForeignKey(PermissionGroup, verbose_name='权限组')
    valid = models.BooleanField(verbose_name='有效', default=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'posts'
        verbose_name = '职位岗位'
        verbose_name_plural = '职位岗位'

class PostHistory(models.Model):
    """岗位任职历史"""
    user = models.ForeignKey(User, verbose_name='用户')
    post = models.ForeignKey(Posts, verbose_name='岗位')
    start_date = models.DateField(verbose_name='任职开始日期', blank=False, default=timezone.now)
    end_date = models.DateField(verbose_name='任职结束日期', blank=True)

    class Meta:
        db_table = 'post_history'
        verbose_name = '岗位任职历史'
        verbose_name_plural = '岗位任职历史'
        unique_together = (("user","post"),)

class ExtraInfo(ProfileUser):
    """系统User表扩展"""
    name = models.CharField(max_length=20, verbose_name='姓名', blank=False, default='')
    login_id = models.CharField(max_length=20, verbose_name='登录账号', blank=False, default='')
    login_pwd = models.CharField(max_length=20, verbose_name='密码', blank=False, default='')
    employee_id = models.CharField(max_length=3,default='', blank=False, verbose_name='员工编号')
    department = models.ForeignKey(UserDepartment, verbose_name='部门', default='', null=True, blank=True)
    contracts = models.CharField(max_length=11, default='', blank=False, verbose_name='手机号')
    address = models.CharField(max_length=200, default='', blank=True, verbose_name='地址')
    identity_code = models.CharField(max_length=18, default='', blank=True, verbose_name='身份证号')
    hire_date = models.DateField(verbose_name='入职日期', default=timezone.now)
    education = models.ForeignKey(Educations, verbose_name='学历', blank=True, null=True)
    post = models.ForeignKey(Posts, verbose_name='职位', blank=False, default=Posts.default_pk)
